package com.swss.g.user.action;

import java.util.List;

import com.swss.g.base.BaseAction;
import com.swss.g.sys.service.SysService;
import com.swss.g.user.bean.Module;
import com.swss.g.user.bean.Popedom;
import com.swss.g.user.bean.Role;
import com.swss.g.user.bean.RoleUser;
import com.swss.g.user.bean.User;
import com.swss.g.user.service.ModuleService;
import com.swss.g.user.service.PopedomService;
import com.swss.g.user.service.RoleService;
import com.swss.g.user.service.UserService;
import com.swss.util.BeanUtils;
import com.swss.util.MD5;
import com.swss.util.Pagination;

public class UserAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3016348148306946228L;
	/**
	 * bean
	 */

	private User user;

	private Pagination pagination;

	private int count;

	private List<User> userList;

	private List<Role> roleList;

	private List<Module> myModList;

	private List<Popedom> popeList;
	private String url;

	private RoleUser roleUser;

	private Role role;
	
	private List chargeOffDeptList;

	/**
	 * service
	 * 
	 * @return
	 */

	private UserService userService;
	private PopedomService popedomService;
	private RoleService roleService;
	private ModuleService moduleService;
	private SysService sysService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String findById() {
		try {

			this.chargeOffDeptList=this.getSysService().getChargeOffDept();
			
			this.user = this.getUserService().get(this.user.getId());
			// this.getUserService().findRoleByUserId(this.user.getId());

			// this.getUserService().findPopesByUserId(this.user.getId());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// return INPUT;
		}

		if (this.type.equals("update")) {
			return "update";
		}
		return "res";
	}

	public String find() {

		try {
			
			
			String deptNo=this.getSessionAttr("user", User.class).getDeptNo();
			
			if(user==null){
				user=this.getSessionAttr("searchBean", null);
			}else{
				this.setSessionAttr("searchBean", user);
			}
		
			pagination = this.getUserService().findUserByLike(pagination, user,deptNo);
			pagination.setUrl("/swss" + this.getRequest().getServletPath());
			
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return INPUT;
		}
		return "res";
	}

	public String add() {
		try {
			MD5 md5 = new MD5();
			this.user.setPassword(md5.getMD5Str(this.user.getPassword()));
			User u = this.getUserService().save(this.user);
			this.roleUser.setUserId(u.getId());
			this.getUserService().saveRoleUser(roleUser);

			// userList = this.getUserService().findAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	};

	public String del() {
		try {

			this.getUserService().delete(user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String update() {
		try {
			User u = this.getUserService().get(user.getId());

			BeanUtils.copyProperties(user, u);
			if (user.getPassword() != null) {
				MD5 md5 = new MD5();
				u.setPassword(md5.getMD5Str(u.getPassword()));
			}

			this.getUserService().update(u);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String count() {
		try {
			count = this.getUserService().findCount();
			System.out.println(count);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String init() {

		return "userInit";
	}

	public String saveInit() {
		try {
			if (user != null) {
				this.user = this.getUserService().get(user.getId());

			}
			this.role = this.userService.findRoleByUserId(48);
			// System.out.println("====="+role.getRoleLevel());
			
			this.chargeOffDeptList=this.sysService.getChargeOffDept();

			pagination = new Pagination();
			pagination.setUsepage(false);
			pagination.setOrderBy("roleLevel");
			pagination.setOrderType("asc");
			this.roleList = this.roleService.findAll(pagination).getResult();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "saveInit";
	}

	public String updatePwdInit() {
		try {
			this.user = this.getUserService().get(user.getId());

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "pwdUpdate";
	}

	public String updateRoleInit() {

		try {

			pagination = new Pagination();
			pagination.setUsepage(false);
			this.roleList = this.roleService.findAll(pagination).getResult();

			List<RoleUser> list = this.getUserService().findRoleUser(roleUser);

			this.roleUser = list.get(0);
			// this.user=this.getUserService().get(roleUser.getId());

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "roleUserUpdate";
	}

	public String updateRoleUser() {
		try {

			RoleUser ru = this.userService.findRoleUserById(roleUser.getId());

			BeanUtils.copyProperties(roleUser, ru);

			this.userService.updateRoleUser(ru);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String findAllUser() {

		try {
			// 临时
			
			this.chargeOffDeptList=this.sysService.getChargeOffDept();
			
			
			String deptNo=this.getSessionAttr("user", User.class).getDeptNo();
            
			pagination = this.getUserService().findUserByLike(pagination, user,deptNo);
			pagination.setUrl("/swss" + this.getRequest().getServletPath());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return INPUT;
		}

		return SUCCESS;
	}

	/**
	 * 展示当前用户顶部导航
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String showTopNav() {
		try {

			// this.user = this.getUserService().get(48);

			
			myModList = this.getUserService().findModulePopesByUserId(
					this.getSessionAttr("user", User.class).getId());
			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public PopedomService getPopedomService() {
		return popedomService;
	}

	public void setPopedomService(PopedomService popedomService) {
		this.popedomService = popedomService;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public RoleUser getRoleUser() {
		return roleUser;
	}

	public void setRoleUser(RoleUser roleUser) {
		this.roleUser = roleUser;
	}

	public ModuleService getModuleService() {
		return moduleService;
	}

	public void setModuleService(ModuleService moduleService) {
		this.moduleService = moduleService;
	}

	public List<Popedom> getPopeList() {
		return popeList;
	}

	public void setPopeList(List<Popedom> popeList) {
		this.popeList = popeList;
	}

	public List<Module> getMyModList() {
		return myModList;
	}

	public void setMyModList(List<Module> myModList) {
		this.myModList = myModList;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public SysService getSysService() {
		return sysService;
	}

	public void setSysService(SysService sysService) {
		this.sysService = sysService;
	}

	public List getChargeOffDeptList() {
		return chargeOffDeptList;
	}

	public void setChargeOffDeptList(List chargeOffDeptList) {
		this.chargeOffDeptList = chargeOffDeptList;
	}

}
